package com.xwb.service.impl;


import com.xwb.entity.Instorage;
import com.xwb.entity.User;
import com.xwb.entity.Warehouse;
import com.xwb.mapper.*;
import com.xwb.service.AdmiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdmiServiceImpl implements AdmiService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private InstorageMapper instorageMapper;
    @Autowired
    private OutstorageMapper outstorageMapper;
    @Autowired
    private ProviderMapper providerMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Warehouse> admiQureyAllWarHouse() {
        List<Warehouse> warehouses = warehouseMapper.selectList(null);
        return warehouses;
    }

    @Override
    public String updateWareHouse(Warehouse warehouse) {
        warehouseMapper.updateById(warehouse);
        return "更新成功";
    }

    @Override
    public String deleteWareHouse(Integer productId) {
        warehouseMapper.deleteById(productId);
        return null;
    }

    @Override
    public List<User> admiQueryAllUser() {
        List<User> users = userMapper.selectList(null);
        return users;
    }

    @Override
    public String updateUser(User user) {
//        User yuanuser = userMapper.selectById(user.getUserid());//获取未修改时的用户信息
//        if(yuanuser.getPassword().equals(user.getPassword())) userMapper.updateById(user);//若密码没有修改，直接更新
//        else{
//            user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));//若密码更新，先对密码加密再更新
//            userMapper.updateById(user);
//        }
        return null;
    }

    @Override
    public String deleteUser(Integer userId) {
        userMapper.deleteById(userId);
        return null;
    }

    @Override
    public List<HashMap<String,Object>> admiQueryAllInstorage() {
        List<Instorage> instorages = instorageMapper.selectList(null);
        ArrayList<HashMap<String,Object>> hashMaps = new ArrayList<>();
        for (Instorage instorage : instorages) {
            HashMap<String, Object> Map = new HashMap<>();
            Map.put("id",instorage.getInstorageid());
            Map.put("productId",instorage.getProductid());
            Map.put("productName",instorage.getProductname());
            Map.put("price",instorage.getPrice());
            Map.put("instorageTime",instorage.getInstoragetime());
            Map.put("number",instorage.getNumber());
            Map.put("providerName",providerMapper.selectById(instorage.getProviderid()).getProvidername());
            Map.put("operatorName",userMapper.selectById(instorage.getOperatorid()).getUsername());
            hashMaps.add(Map);
        }
        return hashMaps;
    }

    @Override
    public List<Map<String,Object>> admiQueryAllOutstorage() {
        List<Map<String, Object>> maps = outstorageMapper.selectMaps(null);
        for (Map<String, Object> map : maps) {
            map.put("providerName",providerMapper.selectById((int)map.get("retailerId")).getProvidername());
            map.put("operatorName",userMapper.selectById((int)map.get("operatorId")).getUsername());
        }
        return maps;
    }
}
